
//===================================================================================================

function P3D_CALLBACKS() {
    // 回调函数，刷新界面
    this.P3D_OnUpdateUICallBack = function () { }
    // 回调函数，使用当前帧数刷新进度条
    this.P3D_OnAnimRefreshCallBack = function (frame) { }
    // 回调函数，设置暂停/继续图标状态，当动画播放完毕时自动设置成暂停状态
    this.P3D_OnAnimFinishCallBack = function (isPause) { }
    // 回调函数，键盘按下事件，发生在P3DToolKit已经执行内部事件之后
    this.P3D_OnKeyboardDownCallBack = function (event) { }
    // 回调函数，键盘抬起事件，发生在P3DToolKit已经执行内部事件之后
    this.P3D_OnKeyboardUpCallBack = function (event) { }
    // 回调函数，鼠标按下事件，发生在P3DToolKit已经执行内部事件之后
    this.P3D_OnMouseDownCallBack = function (event) { }
    // 回调函数，鼠标抬起事件，发生在P3DToolKit已经执行内部事件之后
    this.P3D_OnMouseUpCallBack = function (event) { }
    // 回调函数，鼠标拖拽事件，发生在P3DToolKit已经执行内部事件之后
    this.P3D_OnMouseMoveCallBack = function (event) { }
    // 回调函数，鼠标滚轮事件，发生在P3DToolKit已经执行内部事件之后
    this.P3D_OnMouseWheelCallBack = function (event) { }
    // 回调函数，手机触屏按下事件，发生在P3DToolKit已经执行内部事件之后
    this.P3D_OnTouchStartCallBack = function (event) { }
    // 回调函数，手机触屏滑动事件，发生在P3DToolKit已经执行内部事件之后
    this.P3D_OnTouchMoveCallBack = function (event) { }
    // 回调函数，手机触屏抬起事件，发生在P3DToolKit已经执行内部事件之后
    this.P3D_OnTouchEndCallBack = function (event) { }
    // 回调函数, 摄像机变化, 发生在视图矩阵变化之后
    this.P3D_OnCameraChangeCallback = function() {}
    // 回调函数，模型加载完毕
    this.P3D_OnLoadModelEndCallback = function () { }
    // 回调函数，批注信息创建完毕
    this.P3D_OnCommentCreateCallback = function(commentId) { }
    // 回调函数，批注信息删除完毕
    this.P3D_OnCommentDeleteCallback = function(commentId) { }
    // 回调函数，批注信息选中
    this.P3D_OnCommentSelectCallback = function(commentId) { }
    // 回调函数，导入模型后返回导入的标签值和objectID的偏移值
    this.P3D_OnImportModelCallback = function(importTag, offsetObjId) { }
}

//===================================================================================================

function P3DToolkitLib(dom, callbacks, settings) {
    initComponet(dom);
    initModuleCallbacks(callbacks);
    initSettings(settings);

    this.P3D_Version = P3D_Version;
    this.P3D_CreateComponet = P3D_CreateComponet;
    this.P3D_DestoryComponet = P3D_DestoryComponet;
    this.P3D_OnComponetResize = P3D_OnComponetResize;
    this.P3D_ConfigureLoader = P3D_ConfigureLoader;
    this.P3D_InitData = P3D_InitData;
    this.P3D_UninitData = P3D_UninitData;
    this.P3D_ImportData = P3D_ImportData;
    this.P3D_ExportData = P3D_ExportData;
    this.P3D_DeleteData = P3D_DeleteData;

    this.P3D_GetFileSceneInfo = P3D_GetFileSceneInfo;
    this.P3D_GetFileUnitInfo = P3D_GetFileUnitInfo;

    this.P3D_GetAllObjIDs = P3D_GetAllObjIDs;
    this.P3D_GetSelObjIDs = P3D_GetSelObjIDs;
    this.P3D_SetSelStatusByObjIDs = P3D_SetSelStatusByObjIDs;
    this.P3D_SetClearSelStatus = P3D_SetClearSelStatus;
    this.P3D_GetObjVisible = P3D_GetObjVisible
    this.P3D_SetObjVisible = P3D_SetObjVisible
    this.P3D_GetObjTransparent = P3D_GetObjTransparent;
    this.P3D_SetObjTransparent = P3D_SetObjTransparent;
    this.P3D_SetObjColor = P3D_SetObjColor;
    this.P3D_GetObjName = P3D_GetObjName
    this.P3D_GetObjParams = P3D_GetObjParams
    this.P3D_SetObjsHighlight = P3D_SetObjsHighlight;
    this.P3D_FlickerObjs = P3D_FlickerObjs;
    this.P3D_GetObjsCenterPos = P3D_GetObjsCenterPos;
    this.P3D_SetObjsEnhancedDisplay = P3D_SetObjsEnhancedDisplay;
    this.P3D_SetObjsEnhancedDisplayById = P3D_SetObjsEnhancedDisplayById;
    this.P3D_GetObjBox = P3D_GetObjBox;
    this.P3D_GetObjAttrInfo = P3D_GetObjAttrInfo;
    this.P3D_GetObjModelID = P3D_GetObjModelID;
    this.P3D_SetObjMoveByWindow = P3D_SetObjMoveByWindow;
    this.P3D_SetObjMoveByNavigator = P3D_SetObjMoveByNavigator;
    this.P3D_SetObjMoveByParams = P3D_SetObjMoveByParams;

    this.P3D_AnimPlay = P3D_AnimPlay;
    this.P3D_AnimPause = P3D_AnimPause;
    this.P3D_AnimResume = P3D_AnimResume;
    this.P3D_AnimStop = P3D_AnimStop;
    this.P3D_GetAnimFrames = P3D_GetAnimFrames;
    this.P3D_SetAnimCurFrame = P3D_SetAnimCurFrame;
    this.P3D_GetAnimCurFrame = P3D_GetAnimCurFrame;
    this.P3D_AnimPlayRange = P3D_AnimPlayRange;
    this.P3D_SetAnimCameraOn = P3D_SetAnimCameraOn;

    this.P3D_Home = P3D_Home;
    this.P3D_GetPickElements = P3D_GetPickElements;
    this.P3D_SetElementColor = P3D_SetElementColor;
    this.P3D_ResetElementColor = P3D_ResetElementColor;
    this.P3D_SetBkImage = P3D_SetBkImage;
    this.P3D_SetBkImageUrl = P3D_SetBkImageUrl;
    this.P3D_SetBkColor = P3D_SetBkColor;
    this.P3D_GetBkColor = P3D_GetBkColor;
    this.P3D_SetSelShowFlag = P3D_SetSelShowFlag;
    this.P3D_SetMultSelFlag = P3D_SetMultSelFlag;
    this.P3D_SetObjectCaptureFlag = P3D_SetObjectCaptureFlag;
    this.P3D_GetCaptureObjectID = P3D_GetCaptureObjectID;
    this.P3D_SetGeomCaptureMode = P3D_SetGeomCaptureMode;
    this.P3D_GetGeomCaptureFlag = P3D_GetGeomCaptureFlag;
    this.P3D_SetMouseOperatorMode = P3D_SetMouseOperatorMode;
    this.P3D_SetRenderMode = P3D_SetRenderMode;
    this.P3D_SetMSAAMode = P3D_SetMSAAMode;

    this.P3D_GetModelTreeRootNode = P3D_GetModelTreeRootNode;
    this.P3D_GetModelTreeObjects = P3D_GetModelTreeObjects;
    this.P3D_SetModelTreeObjectsTrans = P3D_SetModelTreeObjectsTrans;

    this.P3D_GetAllSceneAnimNodeData = P3D_GetAllSceneAnimNodeData;
    this.P3D_PlaySceneAnimRange = P3D_PlaySceneAnimRange;
    
    this.P3D_SetUserCanCommentFlag = P3D_SetUserCanCommentFlag;
    this.P3D_SetCommentUserInfo = P3D_SetCommentUserInfo;
    this.P3D_InitComment = P3D_InitComment;
    this.P3D_UnInitComment = P3D_UnInitComment;
    this.P3D_AddComment = P3D_AddComment;
    this.P3D_DelComment = P3D_DelComment;
    this.P3D_DelAllComment = P3D_DelAllComment;
    this.P3D_ImportComment = P3D_ImportComment;
    this.P3D_ExportComment = P3D_ExportComment;
    this.P3D_GetAllCommentIds = P3D_GetAllCommentIds;
    this.P3D_GetCommentInfoById = P3D_GetCommentInfoById;
    this.P3D_AddCommentByData = P3D_AddCommentByData;
    this.P3D_SetCommentActive = P3D_SetCommentActive;
    this.P3D_SetCommentVisible = P3D_SetCommentVisible;

    this.P3D_SetCanMarkFlag  = P3D_SetCanMarkFlag;
    this.P3D_SetMarkUserInfo = P3D_SetMarkUserInfo;
    // this.P3D_InitMark = P3D_InitMark;
    // this.P3D_UnInitMark = P3D_UnInitMark;
    this.P3D_GetAllMarkID = P3D_GetAllMarkID;
    this.P3D_GetMarkType = P3D_GetMarkType;
    this.P3D_GetMarkBase = P3D_GetMarkBase;
    this.P3D_GetTextMark = P3D_GetTextMark;
    this.P3D_GetImageMark = P3D_GetImageMark;
    this.P3D_GetPointMark = P3D_GetPointMark;
    this.P3D_GetLineMark = P3D_GetLineMark;
    this.P3D_GetFoldLineMark = P3D_GetFoldLineMark;
    this.P3D_GetPencilMark = P3D_GetPencilMark;
    this.P3D_GetRectangleMark = P3D_GetRectangleMark;
    this.P3D_GetEllipseMark = P3D_GetEllipseMark;
    this.P3D_GetPolygonMark = P3D_GetPolygonMark;
    this.P3D_GetTextNoteMark = P3D_GetTextNoteMark;
    this.P3D_GetImageNoteMark = P3D_GetImageNoteMark;
    this.P3D_CreateTextMark = P3D_CreateTextMark;
    this.P3D_CreateImageMark = P3D_CreateImageMark;
    this.P3D_CreatePointMark = P3D_CreatePointMark;
    this.P3D_CreateLineMark = P3D_CreateLineMark;
    this.P3D_CreateFoldLineMark = P3D_CreateFoldLineMark;
    this.P3D_CreatePencilMark = P3D_CreatePencilMark;
    this.P3D_CreateRectangleMark = P3D_CreateRectangleMark;
    this.P3D_CreateEllipseMark = P3D_CreateEllipseMark;
    this.P3D_CreatePolygonMark = P3D_CreatePolygonMark;
    this.P3D_CreateTextNoteMark = P3D_CreateTextNoteMark;
    this.P3D_CreateImageNoteMark = P3D_CreateImageNoteMark;
    this.P3D_IsMarkVisible = P3D_IsMarkVisible;
    this.P3D_SetMarkVisible = P3D_SetMarkVisible;
    this.P3D_FocusToMark = P3D_FocusToMark;
    this.P3D_DeleteAllMark = P3D_DeleteAllMark;
    this.P3D_DeleteSelMark = P3D_DeleteSelMark;
    this.P3D_GetSelMarkID = P3D_GetSelMarkID;
    this.P3D_SelMark = P3D_SelMark;
    this.P3D_ClearMarkSelected = P3D_ClearMarkSelected;

    this.P3D_ChangeView = P3D_ChangeView;
    this.P3D_EnableViewAnim = P3D_EnableViewAnim;
    this.P3D_SetViewOnSelObjs = P3D_SetViewOnSelObjs;
    this.P3D_ViewRotate = P3D_ViewRotate;
    this.P3D_ViewScale = P3D_ViewScale;
    this.P3D_ViewSlide = P3D_ViewSlide;
    this.P3D_RecoderView = P3D_RecoderView;
    this.P3D_SetCameraView = P3D_SetCameraView;
    this.P3D_GetCameraView = P3D_GetCameraView;
    this.P3D_CameraLockUpAxis = P3D_CameraLockUpAxis;
    this.P3D_CameraSetProjectType = P3D_CameraSetProjectType;
    this.P3D_CameraControlMode = P3D_CameraControlMode;
    this.P3D_ViewResize = P3D_ViewResize;

    this.P3D_InitMeasure = P3D_InitMeasure;
    this.P3D_UnInitMeasure = P3D_UnInitMeasure;
    this.P3D_SetMeasureMode = P3D_SetMeasureMode;
    this.P3D_GetMeasureUnitIndex = P3D_GetMeasureUnitIndex;
    this.P3D_SetMeasureUnitVisibe = P3D_SetMeasureUnitVisibe;
    this.P3D_DeleteMeasureUnit = P3D_DeleteMeasureUnit;
    this.P3D_ExportMeasureUnit = P3D_ExportMeasureUnit;
    this.P3D_ImportMeasureUnit = P3D_ImportMeasureUnit;

    this.P3D_GetMaterialList = P3D_GetMaterialList;
    this.P3D_GetObjMaterialList = P3D_GetObjMaterialList;
    this.P3D_SetObjMaterialID = P3D_SetObjMaterialID;
    this.P3D_SetObjMaterialIDEx = P3D_SetObjMaterialIDEx;
    this.P3D_SetObjectMaterialBasis = P3D_SetObjectMaterialBasis;
    this.P3D_ClearObjMaterial = P3D_ClearObjMaterial;

    this.P3D_SetP3DBaseURL = P3D_SetP3DBaseURL;

    this.P3D_InitPmi = P3D_InitPmi;
    this.P3D_UnInitPmi = P3D_UnInitPmi;
    this.P3D_GetAnnotViewIDByTreeID = P3D_GetAnnotViewIDByTreeID;
    this.P3D_GetAnnotViewName = P3D_GetAnnotViewName;
    this.P3D_ShiftAnnotView = P3D_ShiftAnnotView;
    this.P3D_SetAnnotVisibleInView = P3D_SetAnnotVisibleInView;
    this.P3D_GetAnnotIDInView = P3D_GetAnnotIDInView;
    this.P3D_GetViewIDByAnnotID = P3D_GetViewIDByAnnotID;
    this.P3D_GetAnnotName = P3D_GetAnnotName;
    this.P3D_GetAnnotType = P3D_GetAnnotType;
    this.P3D_IsAnnotVisible = P3D_IsAnnotVisible;
    this.P3D_SetAnnotVisible = P3D_SetAnnotVisible;
    this.P3D_GetSelAnnotID = P3D_GetSelAnnotID;
    this.P3D_SelectAnnot = P3D_SelectAnnot;
    this.P3D_ClearAnnotSelected = P3D_ClearAnnotSelected;
    this.P3D_ChangeCameraFaceToAnnot = P3D_ChangeCameraFaceToAnnot;
    this.P3D_ChangeCameraFocusToAnnot = P3D_ChangeCameraFocusToAnnot;
    this.P3D_SetPmiDispColor = P3D_SetPmiDispColor;
    this.P3D_SetPmiSelDispColor = P3D_SetPmiSelDispColor;
    this.P3D_ResetPmiDispColor = P3D_ResetPmiDispColor;
    this.P3D_ResetPmiSelDispColor = P3D_ResetPmiSelDispColor;

    this.P3D_InitSection = P3D_InitSection;
    this.P3D_UnInitSection = P3D_UnInitSection;
    this.P3D_SetClipEnable = P3D_SetClipEnable;
    this.P3D_GetClipEnable = P3D_GetClipEnable;
    this.P3D_SetClipVisible = P3D_SetClipVisible;
    this.P3D_GetClipVisible = P3D_GetClipVisible;
    this.P3D_SetClipRevert = P3D_SetClipRevert;
    this.P3D_ResetClip = P3D_ResetClip;
    this.P3D_SetClipMove = P3D_SetClipMove;
    this.P3D_GetClipPosition = P3D_GetClipPosition;
    this.P3D_GetClipLastMove = P3D_GetClipLastMove;
    this.P3D_SetClipRoate = P3D_SetClipRoate;
    this.P3D_GetClipDirection = P3D_GetClipDirection;
    this.P3D_GetClipLastRotate = P3D_GetClipLastRotate;
    this.P3D_SelectClip = P3D_SelectClip;
    this.P3D_GetSelectedClip = P3D_GetSelectedClip;
    this.P3D_SetClipPlaneColor = P3D_SetClipPlaneColor;
    this.P3D_GetClipPlaneColor = P3D_GetClipPlaneColor;
    this.P3D_SetSectionColor = P3D_SetSectionColor;
    this.P3D_SetContoursColor = P3D_SetContoursColor;
    this.P3D_GetSectionPlaneInfo = P3D_GetSectionPlaneInfo;
    this.P3D_ShowSectionPlane = P3D_ShowSectionPlane;

    this.P3D_CreateNvg = P3D_CreateNvg;
    this.P3D_DestoryNvg = P3D_DestoryNvg;
    this.P3D_SetNvgVisible = P3D_SetNvgVisible;
    this.P3D_SetNvgLayout = P3D_SetNvgLayout;

    this.P3D_TwinStart = P3D_TwinStart;
    this.P3D_TwinTerminal = P3D_TwinTerminal;
    this.P3D_SetObjectOriWorldMatrix = P3D_SetObjectOriWorldMatrix;
    this.P3D_TwinFreshObj = P3D_TwinFreshObj;
    this.P3D_GetObjectOriWorldMatrix = P3D_GetObjectOriWorldMatrix;
    this.P3D_GetObjectCurWorldMatrix = P3D_GetObjectCurWorldMatrix;

    this.P3D_ExplodeStart = P3D_ExplodeStart;
    this.P3D_ExplodeUpdate = P3D_ExplodeUpdate;
    this.P3D_ExplodeFinish = P3D_ExplodeFinish;

    this.P3D_GetDefaultViewID = P3D_GetDefaultViewID;
    this.P3D_GetViewData = P3D_GetViewData;
    this.P3D_ChangeViewByID = P3D_ChangeViewByID;

    this.P3D_GetExplodedViewCount = P3D_GetExplodedViewCount;
    this.P3D_GetExplodedDataByIndex = P3D_GetExplodedDataByIndex;
    this.P3D_GetExplodedDataByID = P3D_GetExplodedDataByID;
    this.P3D_ChangeExplodedViewByID = P3D_ChangeExplodedViewByID;
    this.P3D_ChangeExplodedBomInterFlag = P3D_ChangeExplodedBomInterFlag;
    this.P3D_ChangeExplodedBomHeader = P3D_ChangeExplodedBomHeader;
    this.P3D_ChangeExplodedBomInfo = P3D_ChangeExplodedBomInfo;
}

//===================================================================================================

var g_LoadFileTimeTimeID;

//===================================================================================================

// 版本信息
function P3D_Version() {
    return P3DTOOLKIT_VERSION;
}

function P3D_ConfigureLoader(isLoadPmi, isLoadDeom, isLoadSection) {
    g_ExportPMI = isLoadPmi;
    g_ExportSurfaceAndCurve = isLoadDeom;
    g_ExportSection = isLoadSection;
}

// 加载数据
function P3D_InitData(streamdata, licdata) {
    P3D_UninitData();
    return KernelInitData(streamdata, licdata, startRender);    
}

// 清除数据：模型数据、批注、PMI数据等
function P3D_UninitData() {
    clearModel();
    // 释放解析数据流
    ADFUSDKUnInit();
    // 清除标记数据
    clearMarkData();
}

// 导入新模型
function P3D_ImportData(streamdata, licdata) {
    KernelInitData(streamdata, licdata, importModel);
}

// 导出模型
function P3D_ExportData(importTag) {
    console.log("not support");
}

function P3D_DeleteData(importTag, arrObjIDs) {
    return glRunTime.deleteScleModel(importTag);
}

function P3D_GetFileSceneInfo(){
    var sceneInfo = new P3D_FILESCENEINFO();
    sceneInfo._uModelNum = g_sceneData.stuSceneInfo._uModelNum;
	sceneInfo._uObjectNum = g_sceneData.stuSceneInfo._uObjectNum;	
    sceneInfo._uObjTreeNodeNum = g_sceneData.stuSceneInfo._uObjTreeNodeNum;	
    sceneInfo._uModelTriNum = g_sceneData.stuSceneInfo._uModelTriNum;
    sceneInfo._uObjectTriNum = g_sceneData.stuSceneInfo._uObjectTriNum;

    return sceneInfo;
}

function P3D_GetFileUnitInfo(){
    var sceneUnit = new P3D_FILESCENE_UNIT();
    sceneUnit._nLengthUnit = g_sceneData.stuConfig._stuSceneUnit._nLengthUnit;
    sceneUnit._nMassUnit = g_sceneData.stuConfig._stuSceneUnit._nMassUnit;

    return sceneUnit;
}

// 销毁组件，清除所有数据：模型数据、批注、PMI数据、场景数据、背景等所有资源
function P3D_DestoryComponet() {
    destoryComponet();
    // 释放解析数据流
    ADFUSDKUnInit();
}

// 创建组件，包括三维场景所必须的资源
function P3D_CreateComponet(dom) {
    initComponet(dom);
}

// 组件宽高发生变化时调用刷新渲染窗口
// 默认渲染窗口全覆盖组件，故无需外部传入组件宽和高
function P3D_OnComponetResize() {
    onCanvasResize();
}

// 视图切换
function P3D_ChangeView(type) {
    glRunTime.shiftView(type);
}

function P3D_EnableViewAnim(enable) {
    enableCameraAnimation(enable);
}

// 设置聚焦到选中物件上，模型整体围绕所选零件旋转
// 如果未选中零件或装配体，则默认聚焦到模型整体
function P3D_SetViewOnSelObjs() {
    if (glRunTime == null){
        return;
    }
    glRunTime.setFocusOnObject();
}

function P3D_RecoderView(w, h) {
    return glRunTime.recorderViewImage(w, h, 1);
}

// 视图操作：视角旋转
// 屏幕向右为X轴正方向，向上为Y轴正方向，向里为Z轴正方向
// angleX：模型向X轴正方向旋转
// angleY：模型向Y轴正方向旋转
// angleZ：模型饶Z轴正方向旋转，鼠标操作通常为0
function P3D_ViewRotate(angleX, angleY, angleZ) {
    setCameraRotate(angleX, angleY, angleZ);
}

// 视图操作：视角缩放
// scale > 0表示放大一步
// scale < 0表示缩小一步
// 一步：对应于鼠标滚轮滚动一次
function P3D_ViewScale(scale) {
    setCameraZoom(scale);
}

function P3D_ViewSlide(distX, distY, distZ) {
    setCameraSlide(distX, distY);
}

// 模型复位
function P3D_Home(type) {
    if (glRunTime == null){
        return;
    }

    glRunTime.home(type);
}

// 获取当前拾取的元素
function P3D_GetPickElements() {
    var p3dPickElem = new P3D_PICK_ELEMENT();
    transP3DTookkitPickElem(p3dPickElem, glRunTime.curPickUnit);
    return p3dPickElem;
}

function P3D_SetElementColor(elemnetType, isHighlight, r, g, b) {
    if (elemnetType == P3D_ELEMENT_TYPE_PART && isHighlight) {
        return glRunTime.setObjectHighlightMaterial(r, g, b, 1.0);
    } else if (elemnetType == P3D_ELEMENT_TYPE_SURFACE && isHighlight) {
        return glRunTime.setObjectSurfaceHighlightMaterial(r, g, b, 1.0);
    } else if (elemnetType == P3D_ELEMENT_TYPE_PMI_ITEM) {
        if (isHighlight) {
            return P3D_SetPmiSelDispColor(r, g, b);
        } else {
            return P3D_SetPmiDispColor(r, g, b);
        }
    }
    return false;
}

function P3D_ResetElementColor(elemnetType, isHighlight) {
    if (elemnetType == P3D_ELEMENT_TYPE_PART && isHighlight) {
        return glRunTime.resetObjectHighlightMaterial();
    } else if (elemnetType == P3D_ELEMENT_TYPE_SURFACE && isHighlight) {
        return glRunTime.resetObjectSurfaceHighlightMaterial();
    } else if (elemnetType == P3D_ELEMENT_TYPE_PMI_ITEM) {
        if (isHighlight) {
            return P3D_ResetPmiSelDispColor();
        } else {
            return P3D_ResetPmiDispColor();
        }
    }
    return false;
}

function P3D_GetAllObjIDs() {
    if (glRunTime == null){
        let arrIds = new Array();  
        return arrIds;
    }

    var treeRootNode = P3D_GetModelTreeRootNode();
    var uTreeID = treeRootNode._uTreeNodeID;

    return P3D_GetModelTreeObjects(uTreeID);  
}

// 获取选择的物件Id，返回值为array类型
function P3D_GetSelObjIDs() {
    if (glRunTime == null){
        let arrIds = new Array();  
        return arrIds;
    }

    return glRunTime.getPickObjectIds();
}

// 通过物件Id，设置物件为选中状态
function P3D_SetSelStatusByObjIDs(arrObjIDs) {
    if (glRunTime == null){
        return false;
    }
    return glRunTime.pickModelByIDs(arrObjIDs);
}

// 清空选中选中状态
function P3D_SetClearSelStatus() {
    if (glRunTime == null){
        return false;
    }
    
    return glRunTime.pickModelByIDs(null);
}

function P3D_GetObjVisible(objID) {
    if (glRunTime == null){
        return false;
    }
    return glRunTime.getObjectVisibleByID(objID);
}

function P3D_SetObjVisible(arrObjIDs, isVisible) {
    if (glRunTime == null){
        return false;
    }
    if (Number.isInteger(arrObjIDs)) {
        return glRunTime.setObjectVisibleByID(arrObjIDs, isVisible);
    } else if (Array.isArray(arrObjIDs)) {
        return glRunTime.setMultObjectVisibleByID(arrObjIDs, isVisible);
    }
    return false;
}

function P3D_GetObjTransparent(objID) {
    return glRunTime.getObjectTransparentByID(objID);
}

function P3D_GetObjName(objID) {
    if (glRunTime == null){
        return null;
    }

    return glRunTime.getObjectNameByID(objID, g_GLData.GLModelTreeNode);
}

function P3D_GetObjParams(objID) {
    return glRunTime.getObjectParamsByID(objID, g_GLData.GLModelTreeNode);
}

// 设置指定物件的透明渲染状态
// 如果objIDs为null或者元素个数为0，则默认表示操作当前选中模型
function P3D_SetObjTransparent(arrObjIDs, alpha) {
    if (alpha < 0.0 || alpha > 1.0) {
        return;
    }
    glRunTime.setObjectTransparent(arrObjIDs, alpha);
}

// 设置选中物件的颜色
function P3D_SetObjColor(arrObjIDs, r, g, b, a) {
    if (r < 0.0 || r > 1.0 || g < 0.0 || g > 1.0 ||
        b < 0.0 || b > 1.0 || a < 0.0 || a > 1.0) {
        return false;
    }
    if (Number.isInteger(arrObjIDs)) {
        return glRunTime.setObjectColor(arrObjIDs, r, g, b, a);
    } else if (Array.isArray(arrObjIDs)) {
        return glRunTime.setMultObjectColorByID(arrObjIDs, r, g, b, a);
    }
    return false;
}

// 高亮指定的物件
function P3D_SetObjsHighlight(arrObjIDs) {
    glRunTime.setObjectsPickedByIds(arrObjIDs);
}

var g_flickerIndex = 0;
var g_flickerTransparentFlag = false;
var g_flickerTimeID = 0;
var g_flickerObjIDs = null;
function FlickerObjsCallBack(arrObjIDs, count) { 
    if (g_flickerTransparentFlag) {
        P3D_SetObjTransparent(arrObjIDs, 0.1);

    } else {
        P3D_SetObjTransparent(arrObjIDs, 1);
    }

    if (g_flickerIndex == count*2) {
        return clearFlickerTimer()
    }
    g_flickerIndex++;
    g_flickerTransparentFlag = !g_flickerTransparentFlag;
}

function clearFlickerTimer() {
    window.clearInterval(g_flickerTimeID);
    P3D_SetObjTransparent(g_flickerObjIDs, 1);
    g_flickerTimeID = null
    g_flickerObjIDs = null
    g_flickerIndex = 0;
    g_flickerTransparentFlag = true;
}

// 高亮指定的物件
function P3D_FlickerObjs(arrObjIDs, count= 3, halfPeriod = 600) {
    clearFlickerTimer()
    g_flickerObjIDs = arrObjIDs
    g_flickerTimeID = window.setInterval(()=> FlickerObjsCallBack(arrObjIDs, count), halfPeriod);
}

// 获取物件的包围盒中心点
// 输入：objIDs 数组
// 输出：center2Ds 数组 在画布上的二维坐标点，数据类型Point2(x, y)
// 如果某个ID计算失败则对应的数组元素为null
function P3D_GetObjsCenterPos(arrObjIDs) {
    let arrCenters = new Array();
    for (let i = 0; i < arrObjIDs.length; ++i) {
        arrCenters.push(glRunTime.getObjectCenterById(arrObjIDs[i]));
    }
    return arrCenters;
}

// 弱化其余模型的透明度，以增强选择模型的显示效果
// 输入：treeNodeId模型树节点ID
function P3D_SetObjsEnhancedDisplay(treeNodeId) {
    return glRunTime.setObjectEnhancedDisplayByTreeId(treeNodeId);
}

// 弱化其余模型的透明度，以增强选择模型的显示效果
// 输入：模型实例数组ID
function P3D_SetObjsEnhancedDisplayById(arrObjIDs) {
    return glRunTime.setObjectEnhancedDisplayById(arrObjIDs);
}

// 获取指定物件的包围盒
function P3D_GetObjBox(objID) {
    return glRunTime.getObjectBoxById(objID);
}

// 获取模型实例的属性信息
function P3D_GetObjAttrInfo(arrObjIDs) {
    var attrInfo = new ObjectAttrInfo()
    if (glRunTime.getObjectProductInfoByIDs(arrObjIDs, attrInfo)) {
        return attrInfo;
    }
    return null;
}

function P3D_GetObjModelID(objID) {
    return glRunTime.getObjectModelId(objID);
}

// 播放动画
function P3D_AnimPlay() {
    if (g_sceneData != null) {
        setAnimationStart();
    }
}

// 暂停播放
function P3D_AnimPause() {
    if (g_sceneData != null) {
        animPause();
    }
}

// 继续动画
function P3D_AnimResume() {
    if (g_sceneData != null) {
        animRun();
    }
}

// 暂停播放
function P3D_AnimStop() {
    if (g_sceneData != null) {
        animTerminal();
    }
}

// 获取动画总帧数
function P3D_GetAnimFrames() {
    return getTotalFrames();
}

// 获取当前动画播放帧数
function P3D_GetAnimCurFrame() {
    return uCurFrame
}

// 设置从指定帧开始播放
function P3D_SetAnimCurFrame(frame) {
    return setCurFrame(frame);
}

// 设置动画播放倍速
// 可选值：0.5 - 10.0
function P3D_SetAnimSpeed(speed) {
    return setAnimSpeed(speed);
}

// 获取当前动画播放倍速
function P3D_GetAnimSpeed() {
    return getAnimSpeed();
}

// 从指定帧到指定帧播放动画
function P3D_AnimPlayRange(nStartPos, nEndPos) {
    return PlayUsrFrameAnimation(nStartPos, nEndPos);
}

// 设置是否播放摄像机动画
function P3D_SetAnimCameraOn(isOn) {
    setAnimType(true, isOn, true);
}

// 设置背景图片
function P3D_SetBkImage(imageName) {
        // for (let i = 0; i < g_bgImage.length; ++i) {
    //     if (imageName == g_bgImage[i]) {
    //         glRunTime.setBackground(i);
    //         return;
    //     }
    // }
    const imgIndex = g_bgImageFile[imageName];
    if(imgIndex !== undefined) return  glRunTime.setBackground(imgIndex);
    glRunTime.addUsrBackground(g_bgFoder + imageName);
    g_bgImage.push(imageName);
}

function P3D_SetBkImageUrl(imgUrl) {
    glRunTime.addUsrBackground(imgUrl);
}

// 设置背景颜色
function P3D_SetBkColor(r, g, b) {
    glRunTime.setBackgroundColor(r, g, b);
}

// 获取背景颜色
// 只有在背景是颜色填充时有效，返回颜色数组，成员分别为r,g,b颜色分量
// 背景是图片时返回null
function P3D_GetBkColor() {
    return glRunTime.getBackgroundColor();
}

// 设置选中物件时是否高亮成红色以及显示包围盒
function P3D_SetSelShowFlag(flag) {
    if (flag == 0) {
        glRunTime.setBoxShow(false);
        glRunTime.setHighlight(false);
    } else {
        glRunTime.setBoxShow(true);
        glRunTime.setHighlight(true);
    }
}

// 设置多选模式
function P3D_SetMultSelFlag(flag) {
    if (flag == 0) {
        isMultPick = false;
    } else {
        isMultPick = true;
    }
}

// 设置移动选中物件的标记
function P3D_SetObjMoveByWindow(flag) {
    if (flag) {
        isMove = true;
    } else {
        isMove = false;
    }
}

function P3D_SetObjMoveByNavigator(enbale) {
    if (g_objectAxisNvg == null) {
        g_objectAxisNvg = new ObjectAxisNavigator();
    }

    if (enbale) {
        g_objectAxisNvg.bind();
        glRunTime.setHighlight(false);
    } else {
        g_objectAxisNvg.unbind();
        glRunTime.setHighlight(true);
    }
}

function P3D_SetObjMoveByParams(x, y, z) {
    
}

// 设置鼠标动态捕捉物件ID标记
function P3D_SetObjectCaptureFlag(flag) {
    return glRunTime.enableCaptureObject(flag);
}

// 设置基础操作类型
function P3D_SetMouseOperatorMode(mode) {
    if (glRunTime == null){
        return;
    }
    setMouseOperationMode(mode);
}

// 设置渲染模式
function P3D_SetRenderMode(mode) {
    if (glRunTime == null){
        return;
    }
    g_webglControl.SetRenderMode(mode);
    glRunTime.flush();
}

function P3D_SetMSAAMode(mode) {
    glRunTime.msaaChangeed(mode);
}

// 获取模型树根节点
function P3D_GetModelTreeRootNode() {
    return g_GLData.GLModelTreeNode;
}

// 获取模型树节点下的所有自物件id
function P3D_GetModelTreeObjects(treeId) {
    if (glRunTime == null){
        let arrIds = new Array();  
        return arrIds;
    }
    return glRunTime.getSubObjectsByTreeId(treeId);
}

// 平移模型树节点下的所有物件
// x, y ,z分别为世界坐标系方向上的位移分量
function P3D_SetModelTreeObjectsTrans(treeId, x, y, z) {
    return glRunTime.setSubObjectMoveByTreeId(treeId, x, y, z);
}

// 获取所有场景事件信息
function P3D_GetAllSceneAnimNodeData(data) {
    if (g_sceneData != null) {
        for (var i in g_sceneData.stuTimeNodeTreeTop._arrSubNode){
            data[i] = new P3D_SCENEANIMNODE;
			data[i]._uTimeNodeID = g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._uTimeNodeID;
            data[i]._strName = g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._strName;
            data[i]._strNote = g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._strNote;
            data[i]._uStart = g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._uStartFrameID;
            data[i]._uEnd = g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._uStartFrameID+g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._uFrameSize;
            for (var j in g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._arrSubNode){
                data[i]._arrSubNode[j] = new P3D_SCENEANIMNODE;
                data[i]._arrSubNode[j]._uTimeNodeID = g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._arrSubNode[j]._uTimeNodeID;
                data[i]._arrSubNode[j]._strName = g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._arrSubNode[j]._strName;
                data[i]._arrSubNode[j]._strNote = g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._arrSubNode[j]._strNote;
                data[i]._arrSubNode[j]._uStart = g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._arrSubNode[j]._uStartFrameID;
                data[i]._arrSubNode[j]._uEnd = g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._arrSubNode[j]._uStartFrameID+g_sceneData.stuTimeNodeTreeTop._arrSubNode[i]._arrSubNode[j]._uFrameSize;
            }
        }
    }
}

// 播放场景事件动画
function P3D_PlaySceneAnimRange(start, end) {
    g_nAnimationStart = start;
    g_nAnimationEnd = end;

    animTerminal();
    PlaySceneAnimation();
}

// 设置批注的权限，
function P3D_SetUserCanCommentFlag(type) {
    setUsrCommentRight(type);
}

// 设置批注作者信息
function P3D_SetCommentUserInfo(userName, userID) {
    // 内部需要判断权限
    setCommentUserInfo(userName,userID);
}

// 开始批注模式
function P3D_InitComment() {
    setUsrCommentMode(2, 1);
}

// 结束批注模式
function P3D_UnInitComment() {
    setUsrCommentMode(0, 1);
}

// 创建批注信息
function P3D_AddComment() {
    setUsrCommentMode(1, 1);
}

// 删除批注信息
function P3D_DelComment(commentId) {
    // 内部需要判断权限
    if (commentId < 0) {
        // 删除当前鼠标选中的批注
        deleteCommentInput();
    } else {
        deleteCommentById(commentId);
    }
}

// 删除所有批注信息
function P3D_DelAllComment() {
    return deleteAllComment();
}

// 导入批注信息
// 参数xmlDoc: XML Document对象
function P3D_ImportComment(xmlDoc) {
    // 内部需要判断权限
    importComment(xmlDoc);
}

// 导出批注信息
// 返回值: XML Document对象
function P3D_ExportComment(xmlDoc) {
    // 内部需要判断权限
    return exportComment(xmlDoc);
}

// 获取所有批注ID
function P3D_GetAllCommentIds() {
    return getAllCommentIds();
}

function P3D_GetCommentInfoById(commentId) {
    return getCommentInfoById(commentId);
}

function P3D_AddCommentByData(commentInfo) {
    return addCommentByInfo(commentInfo);
}

function P3D_SetCommentActive(arrCommentId, isObjectActive) {
    return setCommentActiveById(arrCommentId, isObjectActive);
}

function P3D_SetCommentVisible(arrCommentId, isVisible) {
    return setCommentVisibleById(arrCommentId, isVisible);
}

// 设置是否可以三维标记的权限
function P3D_SetCanMarkFlag(flag) {
    setCanMarkFlag(flag);
}

// 设置是否可以三维标记的用户名和用户ID
function P3D_SetMarkUserInfo(userName, userID) {
    setMarkUserInfo(userName, userID)
}

// 创建三维标记，markType参考P3DMARK_***
function P3D_CreateMark(markType) {
    const markParms = {
        [P3DMARK_WORLD_POINT]: { type: P3DMARK_POINT, flag: 2},
        [P3DMARK_WORLD_PENCIL]: { type: P3DMARK_PENCIL, flag: 2},
        [P3DMARK_LOCAL_POINT]:{ type: P3DMARK_POINT, flag: 1},
        [P3DMARK_LOCAL_LINE]:{ type: P3DMARK_LINE, flag: 1},
        [P3DMARK_LOCAL_FOLDLINE]:{ type: P3DMARK_FOLDLINE, flag: 1},
        [P3DMARK_LOCAL_RECTANGLE]:{ type: P3DMARK_RECTANGLE, flag: 1},
        [P3DMARK_LOCAL_ELLIPSE]:{ type: P3DMARK_ELLIPSE, flag: 1},
        [P3DMARK_LOCAL_POLYGON]:{ type: P3DMARK_POLYGON, flag: 1},
    }

    let {type = markType, flag = 3} = markParms[markType]
    
    P3D_SetMarkType(type, flag)
}

// 获取三维标记ID
function P3D_GetAllMarkID() {
    return P3D_GetMarkTree().map(i=>i.baseInfo.uID)
}

// 获取三维标记的类型
function P3D_GetMarkType(markID) {
    const mark = P3D_GetMarkTree().find(i=>i.baseInfo.uID === markID)
    function getMarkType(flag, type){
        const markParms = {
            [P3DMARK_WORLD_POINT]: { type: P3DMARK_POINT, flag: 2},
            [P3DMARK_WORLD_PENCIL]: { type: P3DMARK_PENCIL, flag: 2},
            [P3DMARK_LOCAL_POINT]:{ type: P3DMARK_POINT, flag: 1},
            [P3DMARK_LOCAL_LINE]:{ type: P3DMARK_LINE, flag: 1},
            [P3DMARK_LOCAL_FOLDLINE]:{ type: P3DMARK_FOLDLINE, flag: 1},
            [P3DMARK_LOCAL_RECTANGLE]:{ type: P3DMARK_RECTANGLE, flag: 1},
            [P3DMARK_LOCAL_ELLIPSE]:{ type: P3DMARK_ELLIPSE, flag: 1},
            [P3DMARK_LOCAL_POLYGON]:{ type: P3DMARK_POLYGON, flag: 1},
        }
        if(flag ===  3) return type
        return Object.keys(markParms).find(key=>{
            return markParms[key].type =type && markParms[key].flag === flag
        })
    }
    return mark ? getMarkType(mark.markFlag, mark.markType) : null
}

// 获取三维标记的基础信息, 返回值为P3D_BASEMARK
function P3D_GetMarkBase(markID) {
    const mark = P3D_GetMarkTree().find(i=>i.baseInfo.uID === markID)
    return mark ? mark.baseInfo : null
}

// 获取三维文字标记的信息, 返回值为P3D_TEXTMARK
function P3D_GetTextMark(markID) {
    const markList = getMarkDataByType(P3DMARK_TEXT)
    return markList.find(i=>i.baseInfo.uID === markID)
}

// 获取三维图像标记的信息, 返回值为P3D_IMAGEMARK
function P3D_GetImageMark(markID) {
    const markList = getMarkDataByType(P3DMARK_IMAGE)
    return markList.find(i=>i.baseInfo.uID === markID)
}

// 获取三维点标记的信息, 返回值为P3D_POINTMARK
function P3D_GetPointMark(markID) {
    const markList = getMarkDataByType(P3DMARK_POINT)
    return markList.find(i=>i.baseInfo.uID === markID)
}

// 获取三维直线标记的信息, 返回值为P3D_LINEMARK
function P3D_GetLineMark(markID) {
    const markList = getMarkDataByType(P3DMARK_LINE)
    return markList.find(i=>i.baseInfo.uID === markID)
}

// 获取三维折线标记的信息, 返回值为P3D_FOLDLINEMARK
function P3D_GetFoldLineMark(markID){
    const markList = getMarkDataByType(P3DMARK_FOLDLINE)
    return markList.find(i=>i.baseInfo.uID === markID)
}

// 获取三维自由线标记的信息, 返回值为P3D_PENCILMARK
function P3D_GetPencilMark(markID) {
    const markList = getMarkDataByType(P3DMARK_PENCIL)
    return markList.find(i=>i.baseInfo.uID === markID)
}

// 获取三维矩形标记的信息, 返回值为P3D_RECTANGLEMARK
function P3D_GetRectangleMark(markID) {
    const markList = getMarkDataByType(P3DMARK_RECTANGLE)
    return markList.find(i=>i.baseInfo.uID === markID)
}

// 获取三维椭圆标记的信息, 返回值为P3D_ELLIPSEMARK
function P3D_GetEllipseMark(markID) {
    const markList = getMarkDataByType(P3DMARK_ELLIPSE)
    return markList.find(i=>i.baseInfo.uID === markID)
}

// 获取多边形标记的信息, 返回值为P3D_POLYGONMARK
function P3D_GetPolygonMark(markID) {
    const markList = getMarkDataByType(P3DMARK_POLYGON)
    return markList.find(i=>i.baseInfo.uID === markID)
}

// 获取文字批注标记的信息, 返回值为P3D_TEXTNOTEMARK
function P3D_GetTextNoteMark(markID) {
    const markList = getMarkDataByType(P3DMARK_NOTE)
    return markList.find(i=>i.baseInfo.uID === markID)
}

// 获取图像批注记项的信息, 返回值为P3D_IMAGENOTEMARK
function P3D_GetImageNoteMark(markID) {
    const markList = getMarkDataByType(P3DMARK_IMAGENOTE)
    return markList.find(i=>i.baseInfo.uID === markID)
}

// 创建三维文字标记的信息，输入参数P3D_TEXTMARK
function P3D_CreateTextMark(markInfo) {
    const markList = getMarkDataByType(P3DMARK_TEXT)
    markList.push(markInfo)
    g_canvas.draw()
}

// 创建三维图像标记的信息，输入参数P3D_IMAGEMARK
function P3D_CreateImageMark(markInfo) {
    const markList = getMarkDataByType(P3DMARK_IMAGE)
    markList.push(markInfo)
    g_canvas.draw()
}

// 创建三维点标记的信息，输入参数P3D_POINTMARK
function P3D_CreatePointMark(markInfo) {
    const markList = getMarkDataByType(P3DMARK_POINT)
    markList.push(markInfo)
    g_canvas.draw()
}

// 创建三维直线标记的信息，输入参数P3D_LINEMARK
function P3D_CreateLineMark(markInfo) {
    const markList = getMarkDataByType(P3DMARK_LINE)
    markList.push(markInfo)
    g_canvas.draw()
}

// 创建三维折线标记的信息，输入参数P3D_FOLDLINEMARK
function P3D_CreateFoldLineMark(markInfo) {
    const markList = getMarkDataByType(P3DMARK_FOLDLINE)
    markList.push(markInfo)
    g_canvas.draw()
}


// 创建三维自由线标记的信息，输入参数P3D_PENCILMARK
function P3D_CreatePencilMark(markInfo) {
    const markList = getMarkDataByType(P3DMARK_PENCIL)
    markList.push(markInfo)
    g_canvas.draw()
}

// 创建三维矩形标记的信息，输入参数P3D_RECTANGLEMARK
function P3D_CreateRectangleMark(markInfo) {
    const markList = getMarkDataByType(P3DMARK_RECTANGLE)
    markList.push(markInfo)
    g_canvas.draw()
}

// 创建三维椭圆标记的信息，输入参数P3D_ELLIPSEMARK
function P3D_CreateEllipseMark(markInfo) {
    const markList = getMarkDataByType(P3DMARK_ELLIPSE)
    markList.push(markInfo)
    g_canvas.draw()
}

// 创建三维多边形标记的信息，输入参数P3D_POLYGONMARK
function P3D_CreatePolygonMark(markInfo) {
    const markList = getMarkDataByType(P3DMARK_POLYGON)
    markList.push(markInfo)
    g_canvas.draw()
}


// 创建文字批注标记的信息, 返回值为P3D_TEXTNOTEMARK
function P3D_CreateTextNoteMark(markInfo) {
    const markList = getMarkDataByType(P3DMARK_NOTE)
    markList.push(markInfo)
    g_canvas.draw()
}

// 创建图像批注标记的信息, 返回值为P3D_IMAGENOTEMARK
function P3D_CreateImageNoteMark(markInfo) {
    const markList = getMarkDataByType(P3DMARK_IMAGENOTE)
    markList.push(markInfo)
    g_canvas.draw()
}

// 获取三维标记的显示状态
function P3D_IsMarkVisible(markID) {
    const baseInfo = P3D_GetMarkBase(markID)
    if(!baseInfo) return null
    return !!(baseInfo.uAttriFlags & P3DMARK_FLAG_VISIBLE)
    // return baseInfo.uAttriFlags
}

// 设置三维标记的显示/隐藏状态
function P3D_SetMarkVisible(markIDs, visible) {
    markIDs.forEach(markID=>{
        const mark = P3D_GetMarkTree().find(i=>i.baseInfo.uID === markID)
        if(!mark)return;
        const {baseInfo, markType} = mark;
        const flag = visible ? baseInfo.uAttriFlags | P3DMARK_FLAG_VISIBLE : baseInfo.uAttriFlags & ~P3DMARK_FLAG_VISIBLE
        baseInfo.uAttriFlags = flag

        if(markType === P3DMARK_NOTE){
            setDeep(mark, 'baseInfo.uAttriFlags', flag)
            setDeep(mark, 'text.baseInfo.uAttriFlags', flag)
        }
        if(markType === P3DMARK_MEASURE){
            setDeep(mark, 'baseInfo.uAttriFlags', flag)
            setDeep(mark, 'result.baseInfo.uAttriFlags', flag)
            setDeep(mark, 'result.text.baseInfo.uAttriFlags', flag)
        }
    })
    g_canvas.draw()
}

// 聚焦到指定三维标记
function P3D_FocusToMark(markID, useInteractAnim) {
    P3D_SetPickMarkTypeById(markID, null, true)
    onMarkMouseUp()
}

// 删除所有三维标记
function P3D_DeleteAllMark() {
    deleteAllMark()
}

// 删除选中的三维标记
function P3D_DeleteSelMark() {
    const pickType = P3D_GetPickMarkType()
    const pickIndex = P3D_GetPickMarkIndex()
    if(pickType === P3DMARK_NONE || pickIndex < 0)return;
    P3D_DeleteMark(pickType, pickIndex)
}

// 获取选中的三维标记ID
function P3D_GetSelMarkID() {
    return P3D_GetPickMarkUID()
}

// 选中三维标记
function P3D_SelMark(markID) {
    P3D_SetPickMarkTypeById(markID)
    onMarkMouseUp()
}

// 清除所有三维标记的选中状态
function P3D_ClearMarkSelected() {
    P3D_ClearPickMark()
    onMarkMouseUp()
}

// 获取文字属性
// borderAttr : P3DMARK_BORDERATTR, fillclr : P3D_RGBA, textAttr: P3DMARK_TEXTATTRIBUTE, textclr : P3D_RGBA
function P3D_GetDefaultTextMarkAttr(borderAttr, fillclr, textAttr, textclr) {
    const mark = new P3D_TEXTMARK()
    if(mark.getDefaultValue) return mark.getDefaultValue()
    return {
        borderAttr:mark.baseInfo.eBorderAttri,
        fillclr: mark.baseInfo.fillcolor,
        textAttr: mark.fontAttri,
        textclr: mark.color
    }
}

// 设置文字属性
function P3D_SetDefaultTextMarkAttr(borderAttr, fillclr, textAttr, textclr) {
    const mark = new P3D_TEXTMARK()
    const defaultValue= {
        borderAttr: borderAttr || mark.baseInfo.eBorderAttri,
        fillclr: fillclr || mark.baseInfo.fillcolor,
        textAttr: textAttr|| mark.fontAttri,
        textclr:textclr|| mark.color
    }
    P3D_TEXTMARK.prototype.setDefaultValue = function() {
        this.baseInfo.eBorderAttri = defaultValue.borderAttr
        this.baseInfo.fillcolor = defaultValue.fillclr
        this.fontAttri = defaultValue.textAttr
        this.color = defaultValue.textclr
    }
    P3D_TEXTMARK.prototype.getDefaultValue = function() {
        return defaultValue
    }
}

// 获取图像属性,以及背景色
// borderAttr : P3DMARK_BORDERATTR
function P3D_GetDefaultImageMarkAttr(borderAttr) {
    const mark = new P3D_IMAGEMARK()
    if(mark.getDefaultValue) return mark.getDefaultValue()
    return {
        borderAttr: mark.baseInfo.eBorderAttri,
    }
}

// 设置图像属性,以及背景色
function P3D_SetDefaultImageMarkAttr(borderAttr) {
    const mark = new P3D_IMAGEMARK()
    const defaultValue= {
        borderAttr: borderAttr || mark.baseInfo.eBorderAttri,
    }
    P3D_IMAGEMARK.prototype.setDefaultValue = function() {
        this.baseInfo.eBorderAttri = defaultValue.borderAttr
    }
    P3D_IMAGEMARK.prototype.getDefaultValue = function() {
        return defaultValue
    }
}

// 获取文字批注属性
// borderAttr : P3DMARK_BORDERATTR, fillclr : P3D_RGBA, textAttr: P3DMARK_TEXTATTRIBUTE, textclr : P3D_RGBA，lineAttr：P3DMARK_LINEATTR
function P3D_GetDefaultTextNoteMarkAttr(borderAttr, fillclr, textAttr, textclr, lineAttr) {
    const mark = new P3D_TEXTNOTEMARK()
    if(mark.getDefaultValue) return mark.getDefaultValue()

    return {
        borderAttr: mark.baseInfo.eBorderAttri,
        fillclr: mark.baseInfo.fillcolor,
        textAttr: mark.text.fontAttri,
        textclr: mark.text.color,
        lineAttr: mark.baseInfo.eLineAttri
    }


}

// 设置文字批注属性
function P3D_SetDefaultTextNoteMarkAttr(borderAttr, fillclr, textAttr, textclr, lineAttr) {
    const mark = new P3D_TEXTNOTEMARK()
    const defaultValue= {
        borderAttr: borderAttr || mark.baseInfo.eBorderAttri,
        fillclr: fillclr || mark.baseInfo.fillcolor,
        textAttr: textAttr|| mark.text.fontAttri,
        textclr: textclr|| mark.text.color,
        lineAttr: lineAttr|| mark.lineAttri
    }
    P3D_TEXTNOTEMARK.prototype.setDefaultValue = function() {
        this.baseInfo.eBorderAttri = defaultValue.borderAttr
        this.baseInfo.fillcolor = defaultValue.fillclr
        this.text.fontAttri = defaultValue.textAttr
        this.text.color = defaultValue.textclr
        this.baseInfo.lineAttri = defaultValue.lineAttr
    }
    P3D_TEXTNOTEMARK.prototype.getDefaultValue = function() {
        return defaultValue
    
    }
}

// 获取图像批注属性
// borderAttr : P3DMARK_BORDERATTR, lineAttr：P3DMARK_LINEATTR
function P3D_GetDefaultImageNoteMarkAttr(borderAttr, lineAttr) {
    const mark = new P3D_IMAGENOTEMARK()
    if(mark.getDefaultValue) return mark.getDefaultValue()

    return {
        borderAttr: mark.baseInfo.eBorderAttri,
        lineAttr: mark.baseInfo.eLineAttri
    }

}

// 设置图像批注属性
function P3D_SetDefaultImageNoteMarkAttr(borderAttr, lineAttr) {
    const mark = new P3D_IMAGENOTEMARK()
    const defaultValue= {
        borderAttr: borderAttr || mark.baseInfo.eBorderAttri,
        lineAttr: lineAttr|| mark.baseInfo.eLineAttri
    }
    P3D_IMAGENOTEMARK.prototype.setDefaultValue = function() {
        this.baseInfo.eBorderAttri = defaultValue.borderAttr
        this.baseInfo.eLineAttri = defaultValue.lineAttr
    }

    P3D_IMAGENOTEMARK.prototype.getDefaultValue = function() {
        return defaultValue
    }

}

// 获取点属性
// pointAttr: P3DMARK_POINT_***, clr : P3D_RGBA，
function P3D_GetDefaultPointMarkAttr(pointAttr, clr, length) {
    const mark = new P3D_POINTMARK()
    if(mark.getDefaultValue) return mark.getDefaultValue()

    return {
        pointAttr: mark.ePointStyle,
        clr: mark.baseInfo.fillcolor,
        length: mark.nLength
    }

}

// 设置点属性
function P3D_SetDefaultPointMarkAttr(pointAttr, clr, length) {
    const mark = new P3D_POINTMARK()
    const defaultValue= {
        pointAttr: pointAttr || mark.ePointStyle,
        clr: clr || mark.baseInfo.fillcolor,
        length: length || mark.nLength
    }
    P3D_POINTMARK.prototype.setDefaultValue = function() {
        this.ePointStyle = defaultValue.pointAttr
        this.baseInfo.fillcolor = defaultValue.clr
        this.nLength = defaultValue.length
    }

    P3D_POINTMARK.prototype.getDefaultValue = function() {
        return defaultValue
    }

}

// 获取直线属性, lineAttr: P3DMARK_LINEATTR
function P3D_GetDefaultLineMarkAttr(lineAttr) {
    const mark = new P3D_LINEMARK()
    if(mark.getDefaultValue) return mark.getDefaultValue()

    return {
        lineAttr: mark.baseInfo.eLineAttri
    }

}

// 设置直线属性
function P3D_SetDefaultLineMarkAttr(lineAttr) {
    const mark = new P3D_LINEMARK()
    const defaultValue= {
        lineAttr: lineAttr || mark.baseInfo.eLineAttri
    }
    P3D_LINEMARK.prototype.setDefaultValue = function() {
        this.baseInfo.eLineAttri = defaultValue.lineAttr
    }

    P3D_LINEMARK.prototype.getDefaultValue = function() {
        return defaultValue
    }

}

// 获取折线属性, lineAttr: P3DMARK_LINEATTR
function P3D_GetDefaultFoldLineMarkAttr(lineAttr) {
    const mark = new P3D_FOLDLINEMARK()
    if(mark.getDefaultValue) return mark.getDefaultValue()

    return {
        lineAttr: mark.baseInfo.eLineAttri
    }

}

// 设置折线属性
function P3D_SetDefaultFoldLineMarkAttr(lineAttr) {
    const mark = new P3D_FOLDLINEMARK()
    const defaultValue= {
        lineAttr: lineAttr || mark.baseInfo.eLineAttri
    }
    P3D_FOLDLINEMARK.prototype.setDefaultValue = function() {
        this.baseInfo.eLineAttri = defaultValue.lineAttr
    }

    P3D_FOLDLINEMARK.prototype.getDefaultValue = function() {
        return defaultValue
    }

}

// 获取自由线属性, lineAttr: P3DMARK_LINEATTR
function P3D_GetDefaultPencilMarkAttr(lineAttr) {
    const mark = new P3D_PENCILMARK()
    if(mark.getDefaultValue) return mark.getDefaultValue()

    return {
        lineAttr: mark.baseInfo.eLineAttri
    }

}

// 设置自由线属性
function P3D_SetDefaultPencilMarkAttr(lineAttr) {
    const mark = new P3D_PENCILMARK()
    const defaultValue= {
        lineAttr: lineAttr || mark.baseInfo.eLineAttri
    }
    P3D_PENCILMARK.prototype.setDefaultValue = function() {
        this.baseInfo.eLineAttri = defaultValue.lineAttr
    }

    P3D_PENCILMARK.prototype.getDefaultValue = function() {
        return defaultValue
    }

}

// 获取矩形属性
// borderAttr : P3DMARK_BORDERATTR, fillclr : P3D_RGBA
function P3D_GetDefaultRectangleMarkAttr(borderAttr, fillclr) {
    const mark = new P3D_RECTANGLEMARK()
    if(mark.getDefaultValue) return mark.getDefaultValue()

    return {
        borderAttr: mark.baseInfo.eBorderAttri,
        fillclr: mark.baseInfo.fillcolor
    }

}

// 设置矩形属性
function P3D_SetDefaultRectangleMarkAttr(borderAttr, fillclr) {
    const mark = new P3D_RECTANGLEMARK()
    const defaultValue= {
        borderAttr: borderAttr || mark.baseInfo.eBorderAttri,
        fillclr: fillclr || mark.baseInfo.fillcolor
    }
    P3D_RECTANGLEMARK.prototype.setDefaultValue = function() {
        this.baseInfo.eBorderAttri = defaultValue.borderAttr
        this.baseInfo.fillcolor = defaultValue.fillclr
    }

    P3D_RECTANGLEMARK.prototype.getDefaultValue = function() {
        return defaultValue
    }

}

// 获取椭圆属性
// borderAttr : P3DMARK_BORDERATTR, fillclr : P3D_RGBA
function P3D_GetDefaultEllipseMarkAttr(borderAttr, fillclr) {
    const mark = new P3D_ELLIPSEMARK()
    if(mark.getDefaultValue) return mark.getDefaultValue()

    return {
        borderAttr: mark.baseInfo.eBorderAttri,
        fillclr: mark.baseInfo.fillcolor
    }

}

// 设置椭圆属性
function P3D_SetDefaultEllipseMarkAttr(borderAttr, fillclr) {
    const mark = new P3D_ELLIPSEMARK()
    const defaultValue= {
        borderAttr: borderAttr || mark.baseInfo.eBorderAttri,
        fillclr: fillclr || mark.baseInfo.fillcolor
    }
    P3D_ELLIPSEMARK.prototype.setDefaultValue = function() {
        this.baseInfo.eBorderAttri = defaultValue.borderAttr
        this.baseInfo.fillcolor = defaultValue.fillclr
    }

    P3D_ELLIPSEMARK.prototype.getDefaultValue = function() {
        return defaultValue
    }
    
}

// 获取多边形属性
// borderAttr : P3DMARK_BORDERATTR, fillclr : P3D_RGBA
function P3D_GetDefaultPolygonMarkAttr(borderAttr, fillclr) {
    const mark = new P3D_POLYGONMARK()
    if(mark.getDefaultValue) return mark.getDefaultValue()

    return {
        borderAttr: mark.baseInfo.eBorderAttri,
        fillclr: mark.baseInfo.fillcolor
    }

}

// 设置多边形属性
function P3D_SetDefaultPolygonMarkAttr(borderAttr, fillclr) {
    const mark = new P3D_POLYGONMARK()
    const defaultValue= {
        borderAttr: borderAttr || mark.baseInfo.eBorderAttri,
        fillclr: fillclr || mark.baseInfo.fillcolor
    }
    P3D_POLYGONMARK.prototype.setDefaultValue = function() {
        this.baseInfo.eBorderAttri = defaultValue.borderAttr
        this.baseInfo.fillcolor = defaultValue.fillclr
    }

    P3D_POLYGONMARK.prototype.getDefaultValue = function() {
        return defaultValue
    }
    
}

// 设置摄像机参数, 参数P3D_CAMERA
function P3D_SetCameraView(camera) {
    if (glRunTime == null){
        return;
    }

    return setCameraCurView(camera);
}

// 获取当前状态的摄像机参数，P3D_CAMERA
function P3D_GetCameraView() {
    return getCameraCurView();
}

function P3D_CameraLockUpAxis(isLock) {
    if (isLock) {
        CameraController.lockUpAxis(g_cameraController, g_sceneConfig._upAxis);
    } else {
        CameraController.lockUpAxis(g_cameraController, null);
    }
}

// 设置投影模式：
//  0: 表示透视投影（默认）
//  1: 表示正交投影
function P3D_CameraSetProjectType(projType) {
    if (glRunTime == null){
        return;
    }
    Camera.setProjectiveType(g_camera, projType);
    glRunTime.flush();
}

// 设置操作模式：
//  0: 旋转模式（默认）
//  1: 自旋模式
//  2: 平移模式
//  3: 步览模式
//  4: 飞行模式
//  5: 环顾模式
function P3D_CameraControlMode(controlMode) {
    if (glRunTime == null){
        return;
    }

    setCameraControlMode(controlMode);
}

function P3D_ViewResize(width, height) {
    doCanvasResize(width, height);
}

// 初始化测量信息
function P3D_InitMeasure() {
    return startMeasureMode();
}

// 退出测量状态
function P3D_UnInitMeasure() {
    return cancelMeacureMode();
}

// 设置测量模式
// measureMode：如果为NONE时表示取消测量；
// 其余有效值为物件、曲面、曲线、曲线与曲线、点、点与点模式
function P3D_SetMeasureMode(measureMode) {
    return setMeasureMode(measureMode);
}

// 获取当前选取的测量单元索引
function P3D_GetMeasureUnitIndex() {
    return getMeasureUnitIndex();
}

// index: 选取的测量单元索引。
// 如果是-1则表示操作全部测量单元。
// 如果是-2，则表示操作当前已拾取的测量单元。
function P3D_SetMeasureUnitVisibe(index, visible) {
    return setMeasureUnitVisible(index, visible);
}

// index: 选取的测量单元索引。
// 如果是-1则表示操作全部测量单元。
// 如果是-2，则默认操作当前已拾取的测量单元。
function P3D_DeleteMeasureUnit(index) {
    return deleteMeasureUnit(index);
}

// index: 选取的测量单元索引。
// 如果是-1则表示导出全部测量单元。
// 如果是-2则表示导出当前已拾取的测量单元。
function P3D_ExportMeasureUnit(index) {
    return exportMeasureUnits(index);
}

function P3D_ImportMeasureUnit(arrMeasureUnit) {
    return importMeasureUnits(arrMeasureUnit);
}

// 设置鼠标动态捕捉类型
// 实时获取鼠标当前指向的几何元素
// captureMode：如果为NONE则表示关闭捕捉
// 其余有效值为：物件、几何曲面、几何曲线、几何顶点
function P3D_SetGeomCaptureMode(captureMode, isEnable) {
    if (!g_sceneGeomtryOn) {
        return false;
    }
    return glRunTime.setCaptureModeEnable(captureMode, isEnable);
}

function P3D_GetGeomCaptureFlag() {
    return glRunTime.getCaptureFilter();
}

// 获取鼠标动态指向的物件Id
function P3D_GetCaptureObjectID() {
    return motionCaptureObjID;
}

// 获取材质信息列表
function P3D_GetMaterialList() {
    return glRunTime.getObjectMetialList(-1);
}

function P3D_GetObjMaterialList(objID) {
    return glRunTime.getObjectMetialList(objID);
}

// 设置物件的材质
function P3D_SetObjMaterialID(objID, objSubsetIndexs, uMtlID) {
    return glRunTime.setObjectSurfaceMaterialById(objID, objSubsetIndexs, uMtlID);
}

function P3D_SetObjMaterialIDEx(objID, objSubsetIndexs, uMtlID, imageUrl) {
    return glRunTime.setObjectMaterialTexture2D(objID, objSubsetIndexs, uMtlID, imageUrl);
}

function P3D_SetObjectMaterialBasis(objID, objSubsetIndexs, uMtlID, basisBuff) {
    return glRunTime.setObjectMaterialTextureBasis(objID, objSubsetIndexs, uMtlID, basisBuff);
}

// 取消物件的材质
function P3D_ClearObjMaterial(objID) {
    return glRunTime.clearObjectMaterialById(objID);
}

// 设置基础URL
function P3D_SetP3DBaseURL(baseUrl) {
    g_baseUrl = baseUrl;
}

function P3D_InitPmi() {
    if (g_scenePmiManager.init()) {
        glRunTime.curRuntimeMode = P3D_RUNTIME_MODE_PMI;
        return true;
    }
    return false;
}

function P3D_UnInitPmi() {
    g_scenePmiManager.clearPmi();
    glRunTime.curRuntimeMode = P3D_RUNTIME_MODE_NORMAL;
}

// 获取模型树节点的标注视图ID
function P3D_GetAnnotViewIDByTreeID(uTreeID) {
    return g_scenePmiManager.getPmiViewIdByTreeId(uTreeID);
}

// 获取标注视图的名称
function P3D_GetAnnotViewName(uAnnotViewID) {
    return g_scenePmiManager.getPmiViewName(uAnnotViewID);
}

// 切换显示标注视图
function P3D_ShiftAnnotView(uAnnotViewID) {
    return g_scenePmiManager.shiftPmiView(uAnnotViewID);
}

// 设置标注视图内标注的显示/隐藏状态
function P3D_SetAnnotVisibleInView(arrViewIDs, bVisible) {
    return g_scenePmiManager.setPmiViewVisible(arrViewIDs, bVisible);
}

// 获取标注视图内的标注ID
function P3D_GetAnnotIDInView(uAnnotViewID) {
    return g_scenePmiManager.getPmiItemIdByViewId(uAnnotViewID);
}

// 获取标注所属的标注视图ID
function P3D_GetViewIDByAnnotID(uAnnotID) {
    return g_scenePmiManager.getPmiViewIdByItemId(uAnnotID);
}

// 获取标注的名称
function P3D_GetAnnotName(uAnnotID) {
    return g_scenePmiManager.getPmiItemName(uAnnotID);
}

// 获取标注的类型
function P3D_GetAnnotType(uAnnotID) {
    return g_scenePmiManager.getPmiItemType(uAnnotID);
}

// 获取标注的显示状态
function P3D_IsAnnotVisible(uAnnotID) {
    return g_scenePmiManager.getPmiItemVisibleById(uAnnotID) == true ? true : false;
}

// 设置标注的显示/隐藏状态
function P3D_SetAnnotVisible(arrAnnotIDs, bVisible) {
    for (let i = 0; i < arrAnnotIDs.length; ++i) {
        g_scenePmiManager.setPmiItemVisibleById(arrAnnotIDs[i], bVisible);
    }
}

// 获取选中的标注ID
function P3D_GetSelAnnotID() {
    return g_scenePmiManager.getPickedItemId();
}

// 选取标注
function P3D_SelectAnnot(arrAnnotIDs) {
    for (let i = 0; i < arrAnnotIDs.length; ++i) {
        g_scenePmiManager.pickItemById(arrAnnotIDs[i], true);
    }
}

// 清除所有标注的选中状态
function P3D_ClearAnnotSelected() {
    g_scenePmiManager.clearPickItem();
}

// 更新当前摄像机使之正视于指定标注
function P3D_ChangeCameraFaceToAnnot(uAnnotID) {
    return g_scenePmiManager.setFaceToPmiItem(uAnnotID);
}

// 更改摄像机焦点至指定标注
function P3D_ChangeCameraFocusToAnnot(uAnnotID) {
    return g_scenePmiManager.setFocusOnPmiItem(uAnnotID);
}

function P3D_SetPmiDispColor(r, g, b) {
    return g_scenePmiManager.setPmiDisplayColor(r, g, b);
}

function P3D_ResetPmiDispColor() {
    return g_scenePmiManager.resetPmiColor();
}

function P3D_SetPmiSelDispColor(r, g, b) {
    return g_scenePmiManager.setPmiSelectColor(r, g, b);
}

function P3D_ResetPmiSelDispColor() {
    return g_scenePmiManager.resetPmiSelectColor();
}

function P3D_InitSection(callbackFunc) {
    if (glRunTime == null){
        return;
    }
    // 初始化剖切参数
    g_sceneSection.initSection(callbackFunc);
    glRunTime.curRuntimeMode = P3D_RUNTIME_MODE_SECTION;
    g_sceneSection.beginSectionOperation();

    P3D_SetSelShowFlag(0);
}

function P3D_UnInitSection() {
    if (glRunTime == null){
        return;
    }
        // 释放剖切数据
    g_sceneSection.finishSectionOperation();
    g_sceneSection.clearSection();
    glRunTime.curRuntimeMode = P3D_RUNTIME_MODE_NORMAL;

    P3D_SetSelShowFlag(1);
}

function P3D_SetClipEnable(clipIndex, isEnable) {
    if (glRunTime == null){
        return;
    }

    return g_sceneSection.setEnableClipping(clipIndex, isEnable);
}

function P3D_GetClipEnable(clipIndex) {
    return g_sceneSection.getClippingEnable(clipIndex);
}

function P3D_SetClipVisible(clipIndex, isVisible) {
    if (glRunTime == null){
        return;
    }
    return g_sceneSection.setVisibleClipping(clipIndex, isVisible);
}

function P3D_GetClipVisible(clipIndex) {
    if (glRunTime == null){
        return;
    }
    return g_sceneSection.getVisibleClipping(clipIndex);
}

function P3D_SetClipRevert(clipIndex) {
    if (glRunTime == null){
        return;
    }
    return g_sceneSection.setRevertClipping(clipIndex);
}

function P3D_ResetClip(clipIndex) {
    if (glRunTime == null){
        return;
    }

    g_sceneSection.resetClipPlanePos(clipIndex);
    g_sceneSection.resetClipPlaneDir(clipIndex);
    g_sceneSection.operateSectionReset(clipIndex);
}

function P3D_SetClipMove(clipIndex, distance) {
    return g_sceneSection.moveClipPlane(clipIndex, distance);
}

function P3D_GetClipPosition(clipIndex) {
    var pos = new P3D_BASEFLOAT3(0, 0, 0);
    g_sceneSection.getPosClipPlane(clipIndex, pos);
    return pos;
}

function P3D_GetClipLastMove(clipIndex) {
    return g_sceneSection.getDistClipPlane(clipIndex);
}

function P3D_SetClipRoate(clipIndex, angleX, angleZ) {
    return g_sceneSection.rotateClipPlaneAxis(clipIndex, angleX, 0, angleZ);
}

function P3D_GetClipDirection(clipIndex) {
    var dir = new P3D_BASEFLOAT3(0, 0, 0);
    g_sceneSection.getDirAngleClipPlane(clipIndex, dir);
    return dir;
}

function P3D_GetClipLastRotate(clipIndex) {
    var angles = new P3D_BASEFLOAT3(0, 0, 0);
    g_sceneSection.getAngleClipPlane(clipIndex, angles);
    return angles;
}

function P3D_SelectClip(clipIndex) {
    if (glRunTime == null){
        return;
    }
    return g_sceneSection.setPlanePicked(clipIndex);
}

function P3D_GetSelectedClip() {
    return g_sceneSection.getPickedPlane();
}

function P3D_SetClipPlaneColor(index, r, g, b, a) {
    if (g_sceneSection == null) {
        return false;
    }
    return g_sceneSection.setPlaneMaterial(index, r, g, b, a);
}

function P3D_GetClipPlaneColor(index) {
    if (g_sceneSection == null) {
        return null;
    }
    return g_sceneSection.getPlaneMaterialRGBA(index)
}

function P3D_SetSectionColor(r, g, b, a) {
    if (g_sceneSection == null) {
        return false;
    }
    return g_sceneSection.setSectionMaterial(r, g, b, a);
}

function P3D_SetContoursColor(r, g, b) {
    if (g_sceneSection == null) {
        return false;
    }
    return g_sceneSection.setContoursMaterial(r, g, b);
}

// 获取剖切视图
function P3D_GetSectionPlaneInfo() {
    if (g_sceneSection == null) {
        return null;
    }
    return g_sceneSection.getSectionView();
}

// 显示剖切视图
function P3D_ShowSectionPlane(arrSectionPlane) {
    if (g_sceneSection == null) {
        return false;
    }

    if (g_sceneSection.setSectionView(arrSectionPlane)) {
        glRunTime.flush();
        return true;
    }
    return false;
}

function P3D_CreateNvg() {
    return createNavigator();
}

function P3D_DestoryNvg() {
    return destoryNavigator();
}

function P3D_SetNvgVisible(visible) {
    if (visible) {
        return setNavigatorVisible(true);
    } else {
        return setNavigatorVisible(false);
    }
}

function P3D_SetNvgLayout(layout) {
    return setNavigatorLayout(layout);
}


// 启动数字孪生模式
function P3D_TwinStart() {
    if (g_sceneData != null) {
        // 进入数字孪生模式
        isDigitalTwinMode = true;
        glRunTime.home(P3D_HOME_TYPE_ALL);
    }
}

// 退出数字孪生模式
function P3D_TwinTerminal() {
    isDigitalTwinMode = false;
}

/**
 * 数字孪生接口
 * 通过物件ID设置原始物件矩阵
 * @param {*} objID 物件ID
 * @param {*} newMatrix P3D_BASEMATRIX矩阵
 */
function P3D_SetObjectOriWorldMatrix(objID, newMatrix) {
    if (g_sceneData != null) {
        // 关闭其余按钮和模型拾取功能那个，只允许用户进行视角变换和缩放
        if (!isDigitalTwinMode) {
            return;
        }
        glRunTime.setObjectOriWorldMatrix(objID, newMatrix);
    }
}

function P3D_TwinFreshObj() {
    if (g_sceneData != null) {
        // 关闭其余按钮和模型拾取功能那个，只允许用户进行视角变换和缩放
        if (!isDigitalTwinMode) {
            return;
        }

        glRunTime.draw3D();
    }
}

/**
 * 数字孪生接口
 * 通过物件ID获取原始物件矩阵
 * @param {*} objID 物件ID
 */
function P3D_GetObjectOriWorldMatrix(objID) {
    if (g_sceneData != null) {
        return glRunTime.getObjectOriWorldMatrix(objID);
    }
}

/**
 * 数字孪生接口
 * 通过物件ID获取当前物件矩阵
 * @param {*} objID 物件ID
 */
function P3D_GetObjectCurWorldMatrix(objID) {
    if (g_sceneData != null) {
        return glRunTime.getObjectCurWorldMatrix(objID);
    }
}

function P3D_ExplodeStart(arrObjIDs, expMode, expScope, nTotalFrame) {
    return setExplodeStart(arrObjIDs, expMode, expScope, nTotalFrame);
}

function P3D_ExplodeUpdate(frame) {
    return updateExplodeFrame(frame);
}

function P3D_ExplodeFinish() {
    return finishExplode();
}

function P3D_GetDefaultViewID() {
    if (g_sceneData == null) {
        return -1;
    }

    return g_sceneData.ViewMgr.defaultViewID;
}

function P3D_GetViewData() {
    if (g_sceneData == null) {
        return null;
    }

    return g_sceneData.ViewMgr.arrView;
}

function P3D_ChangeViewByID(uViewID) {
    if (g_sceneData == null) {
        return;
    }   
    
    for (var i in g_sceneData.ViewMgr.arrView){
        if (uViewID == g_sceneData.ViewMgr.arrView[i].ID) {

            var viewInfo = g_sceneData.ViewMgr.arrView[i];

            // 模型实例位置信息
            if (viewInfo._vecObjPos.length > 0) {
                // 启动数字孪生模式
                P3D_TwinStart();

                for (var j in viewInfo._vecObjPos) {
                    P3D_SetObjectOriWorldMatrix(viewInfo._vecObjPos[j].objID, viewInfo._vecObjPos[j].matWorld);
                }

                // 更新位置信息
                P3D_TwinFreshObj();

                // 退出数字孪生模式
                P3D_TwinTerminal();
            }

            // 切换摄像机视角
            if (viewInfo.type == P3DVIEWTTYPE_STAND) {
                P3D_ChangeView(viewInfo.value);

            } else if (viewInfo.type == P3DVIEWTTYPE_PMI) {
                P3D_ShiftAnnotView(viewInfo.value);

            } else if (viewInfo.type == P3DVIEWTTYPE_CUSTOM) {
                P3D_SetCameraView(viewInfo.viewCamera);
            }

            // 投影类型
            P3D_CameraSetProjectType(viewInfo.projectionType);

            // 模型实例显示隐藏状态
            if (viewInfo.arrObjID.length > 0) {
                var allOjbIDs = P3D_GetAllObjIDs();

                if (viewInfo.visbleObjFlag == 1) {
                    P3D_SetObjVisible(allOjbIDs, false);
                    P3D_SetObjVisible(viewInfo.arrObjID, true);

                } else {
                    P3D_SetObjVisible(allOjbIDs, true);
                    P3D_SetObjVisible(viewInfo.arrObjID, false);
                }

            }     
            break;
        }
    }
}

function P3D_GetExplodedViewCount() {
    if (g_sceneData == null) {
        return 0;
    }

    return g_sceneData.ExplodedViewMgr.arrExplodedView.length;
}

function P3D_GetExplodedDataByIndex(index) {
    if (g_sceneData == null) {
        return null;
    }

    if (index >= 0 && index < g_sceneData.ExplodedViewMgr.arrExplodedView.length){
        return g_sceneData.ExplodedViewMgr.arrExplodedView[index];
    }

    return null;   
}

function P3D_GetExplodedDataByID(explodedViewID) {
    if (g_sceneData == null) {
        return null;
    }

    for (var i in this.g_sceneData.ExplodedViewMgr.arrExplodedView){
        if (g_sceneData.ExplodedViewMgr.arrExplodedView[i].ID == explodedViewID) {
            return g_sceneData.ExplodedViewMgr.arrExplodedView[i];
        }       
    }

    return null;   
}

function P3D_ChangeExplodedViewByID(explodedViewID) {
    if(explodedViewID === -1) return P3D_RestExploded()
    if (g_sceneData == null) {
        return;
    }

    var viewData = P3D_GetExplodedDataByID(explodedViewID);
    if (viewData == null) {
        return
    }

    // 模型实例位置信息
    P3D_TwinStart();

    // 分解视图objId
    let explodedObjIds = []

    for (var j in viewData.arrObjNode) {
        const objID = viewData.arrObjNode[j].objID
        explodedObjIds.push(objID)
        P3D_SetObjectOriWorldMatrix(objID, viewData.arrObjNode[j].matWorld);
    }

    for (var i in viewData.arrObjGroupNode) {
        for (var j in viewData.arrObjGroupNode[i].arrObjNode) {
            const objID = viewData.arrObjGroupNode[i].arrObjNode[j].objID
            explodedObjIds.push(objID)
            P3D_SetObjectOriWorldMatrix(objID, viewData.arrObjGroupNode[i].arrObjNode[j].matWorld);
        }
    }

    // 更新位置信息
    P3D_TwinFreshObj();

    P3D_TwinTerminal();

    // 切换摄像机视角
    P3D_SetCameraView(viewData.camera);

    // 投影类型
    P3D_CameraSetProjectType(viewData.projectType);

    // 更新分解视图球标信息
    P3D_AddExplodedNote(viewData);

    // 显示分解视图下的模型
    const allObjIds = P3D_GetAllObjIDs();
    P3D_SetObjVisible(allObjIds, false);

    P3D_SetObjVisible(explodedObjIds, true);

    if (!window.P3D_UpdateMaterialList){
        return;
    }

    // 更新零件目录信息    
    const material =  P3D_GetExplodedMaterial(viewData);
    P3D_UpdateMaterialList(material);
}

 // 零件目录模型交互状态标记
 function P3D_ChangeExplodedBomInterFlag() {
    if (g_sceneData == null) {
        return;
    }

    return g_sceneData.ExplodedViewMgr.interFlag;
 }

// 零件目录表头信息
 function P3D_ChangeExplodedBomHeader() {
    if (g_sceneData == null) {
        return;
    }

    return g_sceneData.ExplodedViewMgr.arrBomTable;
}   

// 零件目录详细信息
function P3D_ChangeExplodedBomInfo(viewData) {
    if (g_sceneData == null) {
        return;
    }

    return P3D_GetExplodedMaterial(viewData);
}  


function P3D_RestExploded(){
    P3D_Home(P3D_HOME_TYPE_ALL);
    if(window.P3D_RestExplodedTree)P3D_RestExplodedTree()
}